Welcome back.
In this lecture we're going to take a look at a core principle of object oriented programming called
inheritance.
Now inheritance is basically just how a class can inherit properties and behaviors from another class.
The class that is inheriting properties from another class is known as a subclass, while the class
that is providing the properties and behaviors to inherit is called the superclass.
So let's do a quick example of inheritance.
Using tables and meta tables, I'm going to create a new table called animal.
And this table is going to act as my superclass.
Now what are some properties that all animals will have?
Well, let's say all animals will have a name, right?
We don't know the current name of the animal, so we'll set it to unknown.
And maybe another property we want all animals to have is a sound they make.
So maybe a dog, you know, barks, a cat, meows, things like that.
So we'll just set the silent because again, this is just representing our blueprint for creating an
animal.
And again, this is going to be our superclass.
Now we're going to have to make sure to set the meta method of underscore underscore index equal to
the animal table.
That's because we're going to be using this table as our meta table for another class.
And for it to be able to access these properties, we need to make sure to set the index meta method
to the table itself.
Now what's some functions that all animals will have?
Well, maybe a function that all animals could have is the ability to speak I guess, or emit their
sound.
So we'll call this function speak.
And inside this function we can just print um self dot name.
The name of our animal says and then we can do self dot sound.
So that's what our function will do.
And now we have a basic outline for our superclass.
So let's go ahead and create another class.
And this class is going to be our subclass.
So let's make a class for a cat.
So this is going to be our subclass.
Make sure to set the underscore underscore index meta method to the cat class itself.
Again, because we're going to be using this as a meta table.
And then in order for this class to inherit all of these properties from the animal class, we need
to actually set the meta table for this cat class to the animal class.
Now, our cat class will be able to inherit the properties like the name, the sound, and have access
to this function like speak.
So that means we can go down here and set cat dot name equal to cat.
We can do cat dot sound equal to something like meow.
And because it's going to be our subclass, there's obviously going to be some things that cats can
do that other animals can't do.
So maybe, for example, only a cat can sneak around.
And inside this function we can just print self dot name.
Is sneaking around.
So this function here belongs to our cat class, while this function here belongs to our superclass,
the animal class.
And that means we can go ahead and create a new cat object.
I'll call it my cat.
And what we'll do is we'll call the set Metatable function.
Create a new table.
And set the meta table for this table to the cat class.
So this is going to be our new cat object.
And because we've created this new cat object that means we can call functions like speak on it.
Now, what happens when this function gets called is that the interpreter is going to check to see whether
or not this table here has the speak function in it.
When it checks, it's going to see, hey, this index doesn't exist within the my cat table.
So let's go ahead and check to see if the my cat table has a meta table, which it does.
And that's going to be the cat table.
So it's going to go into the cat table and check to see if it has an underscore underscore index meta
method which it does.
And it's going to check that table that's attached to this meta method to see if it has the speak function.
Unfortunately our cat class doesn't have a speak function.
So it's going to check if the cat table has a meta table with an index meta method, which it does because
we set it right here.
So it's going to go ahead and look inside of the animal class and it'll see hey it has an index for
this table.
It's going to check this table and it'll see hey this table has the speak function.
So let's use this function and it'll pass the original table that we called the speak function on to
the function right here as self.
So that means this function should print cat says meow.
And then we can also call the sneak function on our cat table as well.
So let's go ahead and run the game and let's see what gets printed in the console.
There we go.
We get Cat says meow and Cat is sneaking around.
So this is a very basic example of how you can use inheritance in Roblox Lua.
It's actually pretty simple.
All we need to do is set the meta table for any classes to inherit from another class.
Again, the class that will be inheriting stuff from another class is called the subclass, while the
class that is giving those inheritable items is called the superclass.
So I hope you learned a lot and I'll see you in the next lecture.